/**
 * 
 */
package de.nak.roommgmt;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;

/**
 * A simple date/time converter.
 * @author Stephan Anft
 */
public class DateTimeConverter implements Converter {
	/** The date formatter. */
	private static final DateFormat dateFormatter = new SimpleDateFormat("dd.MM.yyyy HH:mm");

	/** {@inheritDoc} */
	@SuppressWarnings("unchecked")
	public Object convert(Class targetType, Object value) throws ConversionException {
		if (value == null || value.equals("")) {
			return null;
		}
		if (value instanceof Date) {
			return value;
		}
		if (!(value instanceof String)) {
			throw new ConversionException("Conversion not supported.");
		}
		try {
			return dateFormatter.parse((String)value);
		}
		catch (Exception e) {
			// Conversion was not successful
			throw new ConversionException("Conversion was not successful.");
		}
	}

}
